

#import "FAMHeightViewController.h"
@interface FAMHeightViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *FAMPageBackView;
@property (weak, nonatomic) IBOutlet UIButton *FAMFtButton;
@property (weak, nonatomic) IBOutlet UIButton *FAMCmButton;
@property (weak, nonatomic) IBOutlet UIPickerView *FAMFtPickerView;
@property (nonatomic, strong)NSMutableArray *FAMFtArray;
@property (nonatomic, strong)NSMutableArray *FAMInArray;
@property (nonatomic, strong)NSMutableArray *FAMCmArray;
@property (nonatomic, assign)NSInteger FAMFt,FAMIn;
@end

@implementation FAMHeightViewController

- (NSMutableArray *)FAMFtArray{
    if (!_FAMFtArray) {
        _FAMFtArray = [NSMutableArray array];
        for (int i = 2; i <= 8; i++) {
            [_FAMFtArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMFtArray;
}
- (NSMutableArray *)FAMInArray{
    if (!_FAMInArray) {
        _FAMInArray = [NSMutableArray array];
        for (int i = 0; i<= 9; i++) {
            [_FAMInArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMInArray;
}
- (NSMutableArray *)FAMCmArray{
    if (!_FAMCmArray) {
        _FAMCmArray = [NSMutableArray array];
        for (int i = 50; i<= 250; i++) {
            [_FAMCmArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMCmArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.FAMPageBackView.layer.shadowColor = kCustom0xColor(0x000000, 1.0).CGColor;
    self.FAMPageBackView.layer.shadowOpacity = 0.15;
    self.FAMPageBackView.layer.shadowRadius = 1.5;
    self.FAMPageBackView.layer.shadowOffset = CGSizeMake(2, 2);
    self.FAMFtPickerView.delegate = self;
    self.FAMFtPickerView.dataSource = self;
    if (self.FAMFtButton.isSelected) {
        self.FAMFt = 6;
        self.FAMIn = 5;
        [self.FAMFtPickerView selectRow:4 inComponent:0 animated:YES];
        [self.FAMFtPickerView selectRow:5 inComponent:1 animated:YES];
        float height = 6 * ft + 5 * ins;
        [self.FAMFoodmateUserdataDictionary setValue:[NSString stringWithFormat:@"%0.2f",height] forKey:@"height"];
    }else{
        [self.FAMFtPickerView selectRow:115 inComponent:0 animated:YES];
        [self.FAMFoodmateUserdataDictionary setValue:@"164" forKey:@"height"];
    }
}
- (IBAction)FAM_backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)FAM_ftButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMCmButton.backgroundColor = kClearColor;
    [self.FAMCmButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMCmButton setSelected:NO];
    [self.FAMFtPickerView reloadAllComponents];
    [self.FAMFtPickerView selectRow:4 inComponent:0 animated:YES];
    [self.FAMFtPickerView selectRow:5 inComponent:1 animated:YES];
    self.FAMFt = 6;
    self.FAMIn = 5;
    float height = 6 * ft + 5 * ins;
    [self.FAMFoodmateUserdataDictionary setValue:[NSString stringWithFormat:@"%0.2f",height] forKey:@"height"];
}
- (IBAction)FAM_cmButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMFtButton.backgroundColor = kClearColor;
    [self.FAMFtButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMFtButton setSelected:NO];
    [self.FAMFtPickerView reloadAllComponents];
    [self.FAMFtPickerView selectRow:114 inComponent:0 animated:YES];
    [self.FAMFoodmateUserdataDictionary setValue:@"164" forKey:@"height"];
}
- (IBAction)FAM_nextButtonClick:(UIButton *)sender {
    FAMWeightViewController *vc = [FAMWeightViewController new];
    vc.FAMFoodmateUserdataDictionary = self.FAMFoodmateUserdataDictionary;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ---- UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (self.FAMFtButton.isSelected) {
        return 2;
    }else{
        return 1;
    }
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            return self.FAMFtArray.count;
        }else{
            return self.FAMInArray.count;
        }
    }else{
        return self.FAMCmArray.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            return [NSString stringWithFormat:@"%@ft",self.FAMFtArray[row]];
        }else{
            return [NSString stringWithFormat:@"%@in",self.FAMInArray[row]];
        }
    }else{
        return [NSString stringWithFormat:@"%@cm",self.FAMCmArray[row]];
    }
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            self.FAMFt = ((NSString *)self.FAMFtArray[row]).integerValue;
        }else{
            self.FAMIn = ((NSString *)self.FAMInArray[row]).integerValue;
        }
        float height = self.FAMFt * ft + self.FAMIn * ins;
        self.FAMFoodmateUserdataDictionary[@"height"] = [NSString stringWithFormat:@"%0.2f",height];
    }else{
        self.FAMFoodmateUserdataDictionary[@"height"] = self.FAMCmArray[row];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}
@end
